package com.cjx.yuanshenstartforumbackend.exception;

import com.cjx.yuanshenstartforumbackend.common.BaseResponse;
import com.cjx.yuanshenstartforumbackend.common.ErrorCode;
import com.cjx.yuanshenstartforumbackend.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description 全局异常处理器
 * @Author cjx
 * @Date 2023/12/21 15:03
 **/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(MyException.class)
    public BaseResponse<?> myExceptionHandler(MyException e){
        log.error("myException:"+e.getMessage(),e);
        return ResultUtils.error(e.getCode(),e.getMessage(),e.getDescription());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<?> runtimeExceptionHandler(RuntimeException e){
        log.error("runtimeException:"+e.getMessage(),e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,e.getMessage(),"");
    }

}
